<?php

/*

    ThunderZ - Web Application

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

Yii::import('application.models.thunderz.profile.TUser');

/**
 * User identity based on database table
 */
class TUserIdentity extends CUserIdentity {

    /**
     * Create a user identity manager with the user info specified
     *
     * @param string $username User name
     * @param string $password User password
     */
    public function MUserIdentity($username, $password) {
        parent::__construct($username,$password);
    }

    /**
     * Authenticates a user based on username and password.
     *
     * @return boolean whether authentication succeeds.
     */
    public function authenticate() {
        Yii::log('[Auth] Authenticate ' . $this->username,
            CLogger::LEVEL_INFO, 'application.thunderz.auth.TUserIdentity');

        // Set attributes
        $attributes = array('username'=>$this->username,
            'password'=>$this->password);

        // Get user with the info specified
        $user = TUser::model()->findAllByAttributes($attributes);

        // If no user present error
        if($user == null) {
            $this->errorCode = CUserIdentity::ERROR_UNKNOWN_IDENTITY;
        }
        else {
            $this->errorCode = CUserIdentity::ERROR_NONE;
        }

        // Return boolean
        return !$this->errorCode;
    }
}

?>
